Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add singleton support for ease of use #331

Closed
wants to merge 1 commit into from

Conversation

MJacred
Copy link
Contributor

@MJacred MJacred commented Aug 28, 2024

copied from my README.md changes:

Use the singleton to ease usage.

bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`
HelloCookie = "Hello Cookie!"

[Cookies]
one = "I have {{.PluralCount}} cookie!"
other = "I have {{.PluralCount}} cookies!"

[CookiesX]
other = "I have {{.X}} cookies!"

[CookiesX2]
one = "I have {{.PluralCount}} {{.X}} cookie!"
other = "I have {{.PluralCount}} {{.X}} cookies!"

[CookiesABC]
other = "I have {{.A}} cookies in my {{.B}} back at {{.C}}!"

[CookiesABC2]
other = "I have {{.PluralCount}} {{.A}} cookies in my {{.B}} back at {{.C}}!"
`), "en.toml")

localizer := i18n.NewLocalizer(bundle, "en-US")
i18n.SetLocalizerInstance(localizer)
i18n.SetABCParams([]string{"A", "B", "C"})

i18n.Localize("HelloCookie") // "Hello Cookie!"
i18n.LocalizePlural("Cookies", 4) // "I have 4 cookies!"
i18n.LocalizeTemplateSingle("CookiesX", "X", "chocolate") // "I have chocolate cookies!"
i18n.LocalizeTemplateSingleWithPlural("CookiesX2", 4, "X", "chocolate") // "I have 4 chocolate cookies!"
i18n.LocalizeTemplateX("CookiesABC", "chocolate", "basket", "home") // "I have chocolate cookies in my basket back at home!"
i18n.LocalizeTemplateXPlural("CookiesABC2", 400, "chocolate", "basket", "home") // "I have 400 chocolate cookies in my basket back at home!"

@MJacred
Copy link
Contributor Author

MJacred commented Sep 2, 2024

@nicksnyder, if you could take a look, please.
I can also add build tags, if you'd like

@nicksnyder
Copy link
Owner

You may certainly create a singleton for your application if you wish, but I am not going to add a singleton API to this project. v1 of this project used a singleton API and it caused various issues, and v2 explicitly moved away from a singleton interface.

@nicksnyder nicksnyder closed this Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants